DMelt:Numeric/5 Integration

From HandWiki
Member

Integration

Integral assigns numbers to functions in a way that can describe displacement, area, volume, and other concepts that arise by combining infinitesimal data. You can use DataMelt to perform numeric integration. It contains several Java libraries to do this.

In addition to the above, DataMelt has several classes to perform integration. They are based on jhplot.F1D jhplot.F1D class. Let us give a small example showing how to integrate [math]\displaystyle{ cos(x)^3 }[/math] using a trapezium rule. We will integrate this function between 1 and 10 using 10k iterations.

from jhplot import F1D
f1=F1D('cos(x)^3')
print f1.integral(10000,1,10)

The output is "-1.13321491381".

Let us perform integration of a function [math]\displaystyle{ \sin(1.0/x)*x^2+10*\cos(x)^3 }[/math] using 5 alternative methods: Gauss4, Gauss8, Richardson, Simpson, Trapezium. The code that does a benchmark of all 5 methods is given below:

from jhplot import *
import time
f1=F1D('sin(1.0/x)*x^2+10*cos(x)^3')
methods=['gauss4', 'gauss8',  'richardson',\
         'simpson','trapezium']
for m in methods:
   start = time.clock()
   d=f1.integral(m,10000,1,10)
   t = time.clock()-start
   print m+' =',d,' time (ms)=',t*1000

The output of the above code is:

gauss4 = 49.1203116758            time (ms)= 155.088374
gauss8 = 49.1203116758            time (ms)= 57.245523
richardson = 49.1203116758        time (ms)= 53.369496
simpson = 49.1203116758           time (ms)= 27.088362
trapezium = 49.1203116663         time (ms)= 20.023047

Integrating rational functions

The Jas integration Jas integration package allows integration of rational functions.

Thus, a function is called rational if it is written as $A(x)/B(x)$, where A and B are polynomials. In this section we will show how to integrate rational functions symbolically.

from edu.jas.poly import *
from edu.jas.arith import *
from edu.jas.integrate import *

br =BigRational(0)
vars = [ "x" ]
fac = GenPolynomialRing(br, len(vars), TermOrder(TermOrder.INVLEX), vars);
eInt = ElementaryIntegration(br)
a = fac.parse("x^7 - 24 x^4 - 4 x^2 + 8 x - 8")
print "A=",a.toString()
b = fac.parse("x^8 + 6 x^6 + 12 x^4 + 8 x^2");
print "B=",b.toString()
gcd = a.gcd(b) # calculate QCD ratio
ret = eInt.integrateHermite(a, b);
print "Result: ", ret[0] ," , " , ret[1]

The answer is:

A=x^7 - 24 x^4 - 4 x^2 + 8 x - 8 
B=x^8 + 6 x^6 + 12 x^4 + 8 x^2
Result:  [1 , x, 6 x, x^4 + 4 x^2 + 4 , ( -1 ) x + 3 , x^2 + 2 ]  ,  [0, 1 , x]